var catId = getUrlParam('catId');

function init() {

    $.ajax({
        type: "post",
        url: ajaxUrl + "/mobile/index/headData",
        async: false,
        data: {},
        success: function (data) {
            var result = data.result;
            var pc = result.pc;
            // console.log(pc);
            var catstr = ' <a href="index.html"><div  class="nav-item nav-hook">' +
                '<span class="text">今日特卖</span>' +
                '</div></a>'
            for (var i = 0; i < pc.length; i++) {
                catstr += ' <div cid="' + pc[i].catId + '" onclick="toChild(' + pc[i].catId + ')" class="nav-item nav-hook">' +
                    '<span class="text">' + pc[i].name + '</span>' +
                    '</div>'
            }
            //        console.log(catstr);
            $('#catList').html(catstr);

            var $cart = $('.nav-item ');
            //console.log(cart.length);
            $cart.each(function () {
                var $this = $(this);
                var cid = $this.attr("cid");
                if (cid == catId) {
                    $this.addClass('on')
                }
                //  console.log(cid);
            });


            $('.nav-section .nav-item').click(function () {
                $(this).addClass('on').siblings().removeClass('on');
            })
        }
    });
    showChild();
    showProduct();
}

function showProduct() {
    //  /mobile/product/productList

    $.ajax({

        type: "post",
        url: ajaxUrl + "/mobile/product/productList",
        async: true,
        data: {'pcatId': catId, 'memberId': memberId, 'page': 1, 'rows': 100},
        success: function (data) {
            var result = data.result;
            var str = '';
            console.log(data);
            for (var i = 0; i < result.length; i++) {
                str += ' <div class="list2-item bottom-border">' +
                    '        <a href="details.html?id='+result[i].productId+'" class="text-img">' +
                    '            <div class="img-wrapper">' +
                    '                <img src="'+result[i].image+'" alt="" class="img">' +
       //             '                <img src="img/qiang-tag.png" alt="" class="tag">' +
                    '            </div>' +
                    '            <div class="text">' +
                    '                <div class="text-top">' +
                    '                    <div class="name">'+result[i].productName+'</div>' +
                    '                </div>' +
                 //   '                <div class="old-price">市价¥32.00</div>' +
                     '                    <div class="sell">销量'+result[i].saleCount+'</div>' +
                    '                <div class="now-price">会员价:￥<i>'+result[i].vipPrice+'</i></div>' +
                    '            </div>' +
                    '        </a>' +
                    '        <div class="add-cart-wrapper">' +
                    '            <div class="add-cart"></div>' +
                    '        </div>' +
                    '    </div>';
            }
              $('#proList').html(str);
        }

    });

}

function showChild() {
    $.ajax({

        type: "post",
        url: ajaxUrl + "/mobile/product/category/getChildrenCategory",
        async: true,
        data: {'catId': catId, 'memberId': memberId, 'page': 1, 'rows': 100},
        success: function (data) {
            var result = data.result;
            var childStr = '';
            //  window.location.href = 'more.html?catId='+id+'&pcatId='+result[0].id;
            for (var i = 0; i < result.length && i < 7; i++) {
                childStr += ' <a href="more.html?catId='+catId+"&pcatId="+result[i].id+'" class="classi-item">' +
                    '<img src="' + result[i].logo + '" class="icon" alt="">' +
                    '<div class="text">' + result[i].name + '</div> </a>'
            }
            childStr += '<a href="more.html?catId='+catId+'" class="classi-item">' +
                '<img src="img/more.png" class="icon" alt="">' +
                '<div class="text">查看更多</div> </a>'


            $('#childList').html(childStr);

            console.log(result);
            // window.location.href = 'more.html?catId=' + id + '&pcatId=' + result[0].id;
        }
    });
}

function toCat(id) {
    $.ajax({

        type: "post",
        url: ajaxUrl + "/mobile/product/category/getChildrenCategory",
        async: true,
        data: {'catId': id},
        success: function (data) {
            var result = data.result;
            console.log(result);
            window.location.href = 'more.html?catId=' + id + '&pcatId=' + result[0].id;
        }
    });
}

function addCart(productId) {
    $.ajax({
        type: "post",
        url: ajaxUrl + '/mobile/carItem/addProduct',
        async: true,
        //productId=412&memberId=623&normalId=508&num=1
        data: {'memberId': memberId, 'productId': productId, 'normalId': null, 'num': 1},
        success: function (data) {
            if (data.code == 0) {
                alertMsg('成功加入购物车');
            } else {
                alertMsg(data.message);
            }
            $('.spec-section').removeClass('on');
            $('.mask').hide();
            cartNum();
        },
        error: function () {
            errorTip();
        },
        complete: function () {

        }
    });
}

function proDetail(id) {
    window.location.href = 'details.html?id=' + id;
}

function toChild(cid) {
    catId=cid
    init();
    //window.location.href = 'index2.html?catId=' + cid;
}